<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <table>
        <tr>
            <td><label for="">标题：</label></td>
            <td><input type="text" placeholder="请输入标题" name="headline"></td>
        </tr>
        <tr>
            <td><label for="">摘要：</label></td>
            <td><input type="text" placeholder="请输入摘要" name="abstract"></td>
        </tr>
        <tr>
            <td><label for="">内容：</label></td>
            <td><input type="text" placeholder="请输入内容" name="content"></td>
        </tr>
        <tr>
            <td><label for="">分类：</label></td>
            <td><input type="text" placeholder="请输入分类" name="classify"></td>
        </tr>
        <tr>
            <td><label for="">作者：</label></td>
            <td><input type="text" placeholder="请输入作者" name="author"></td>
        </tr>
        <tr>
            <td><input type="button" value="保存" onclick="save()"></td>
            <td><input type="button" value="取消并返回" onclick="cancel()"></td>
        </tr>
    </table>
    <script src="./js/jquery-3.6.0.min.js"></script>
    <script src="./js/init.js"></script>
    <script src="./js/api.js"></script>
    <script src="./js/addOredit.js"></script>
    <script>
        $(function () {
    let id = getUrlParam('id');
    id = id || 0;
    getProductById(id).then(res => {
        let obj = res.data;
       $('[name=headline]').val(obj.headline); 
       $('[name=abstract]').val(obj.abstract); 
       $('[name=content]').val(obj.content); 
       $('[name=classify]').val(obj.classify); 
       $('[name=author]').val(obj.author); 

    })
})
 //获取url中的参数 其中name指的是url中的键的名称
 function getUrlParam(name) {

    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象

    var r = window.location.search.substr(1).match(reg); //匹配目标参数

    if (r != null) {
        return unescape(r[2]);
    } else {
        return null; //返回参数值
    }

}
function save() {
    let id = getUrlParam('id');
    let obj = {
        Name: $('[name=headline]').val(),
        sex: $('[name=abstract]').val(),
        sum:$('[name=content]').val(),
        sum:$('[name=classify]').val(),
        sum:$('[name=author]').val(),
    }
    if (id) {
        updateProduct(id, obj).then(res => {
            location.href = './index.html';
        })
    } else {
        let params = {
            headline: obj.headline,
            abstract: obj.abstract,
            content: obj.content,
            classify: obj.classify,
            author: obj.author,
        }
        addProduct(params).then(res => {
            location.href = './index.html';
        })
    }
}
function cancel() {
    location.href = './index.html';
}
    </script>
</body>
</html>